/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker.type;


import java.util.List;

import dex.misc.Strings;


/**
 * A function type.
 */
final public class FunctionType extends Type {
	

	/**
	 * The return type of the function.
	 */
	private Type returnType;
	
	
	/**
	 * The parameter types of the function.
	 */
	private List<Type> parameterTypes;
	

	/**
	 * Constructs a new function type occurrence.
	 * 
	 * @param resultType  the return type of the function
	 * @param parameterTypes  the parameter types of the function
	 */
	FunctionType(TypeManager m, Type resultType, List<Type> parameterTypes) {
		super(m);
		if (resultType == null) {
			throw new IllegalArgumentException();
		}
		if (parameterTypes == null) {
			throw new IllegalArgumentException();
		}
		this.returnType = resultType;
		this.parameterTypes = parameterTypes;
	}


	/**
	 * Returns false, since Dex program instructions must live on the heap.
	 * 
	 * @return false
	 */
	@Override
	public boolean isLocal() {
		return false;
	}
	
	
	/**
	 * Returns true since function types are composed of other types.
	 * 
	 * @return true
	 */
	@Override
	public boolean isCompound() {
		return true;
	}


	/**
	 * Returns the parameter types of the function described by this type.
	 *  
	 * @return  the parameter types
	 */
	public List<Type> getParameterTypes() {
		return parameterTypes;
	}


	/**
	 * Returns the result type of the function described by this type.
	 * The returned type may be null, if the function is a <code>void</code>
	 * function.
	 * 
	 * @return the return type, or null if the function is void
	 */
	public Type getReturnType() {
		return returnType;
	}


	@Override
	public boolean canAssignTo(Type formal) {
		if (!(formal instanceof FunctionType)) {
			return false;
		}
		FunctionType ft = (FunctionType)formal;
		
		if (ft.parameterTypes.size() != parameterTypes.size()) {
			return false;
		}
		
		if (!passable(returnType, ft.returnType)) {
			return false;
		}
		
		for (int i = 0; i < parameterTypes.size(); i++) {
			Type actualParam = parameterTypes.get(i);
			Type formalParam = ft.parameterTypes.get(i);
			if (!passable(actualParam, formalParam)) {
				return false;
			}
		}
		return true;
	}

	
	private boolean passable(Type actual, Type formal) {
		if (actual.isLocal()) {
			return actual == formal;
		}
		return actual.canAssignTo(formal);
	}


	@Override
	public String toString() {
		return returnType + "<" + Strings.list(parameterTypes);
	}
}
